<?php

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\service\UserAddressService;
use app\BaseController;
use app\Response;
use think\facade\Request;
use app\api\validate\Address as apiAddress;

class Address extends BaseController
{
    //地址列表
    public function addressList(){
        if(Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::param("page", 1);
            $num = Request::param("num", 10);
            $default = Request::param("default", "");
            list($data, $count) = UserAddressService::getAddressList($page, $num, $user_id, $default);
            return Response::wrapData($data, $count);
        }
    }
    //地址详情
    public function addressInfo()
    {
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::param("id", "");
            $data = UserAddressService::getAddressInfo($id, $user_id);
            return Response::wrapData($data);
        }
    }

    //地址新增
    public function addressAdd(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::param("id","");
            $phone = Request::param("phone","");
            $name = Request::param("name","");
            $province = Request::param("province","");
            $city = Request::param("city","");
            $area = Request::param("area","");
            $street = Request::param("street","");
            $address = Request::param("address","");
            $default = Request::param("default",0);
            try {
                validate(apiAddress::class)->check([
                    'phone' => $phone,
                    'name' => $name,
                    'province' => $province,
                    'city' => $city,
                    'area' => $area,
                    'address' => $address,
                    'default' => $default,
                ]);
            } catch (\Exception $e) {
                // 验证失败 输出错误信息
                return Response::wrapError(lang($e->getMessage()));
            }
            $data = [
                'phone' => $phone,
                'name' => $name,
                'uid' => $user_id,
                'province' => $province,
                'city' => $city,
                'area' => $area,
                'address' => $address,
                'street' => $street,
                'default' => $default,
                'addtime' => time()
            ];
            if(empty($id)){
                $data["addtime"] = time();
            }
            $res = UserAddressService::addressAddService($id,$user_id,$data);
            if($res){
                return Response::wrapData();
            }else{
                return Response::wrapError("地址新增失败");
            }
        }
    }

    //地址删除
    public function addressDel(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::param("id", "");
            $res = UserAddressService::getAddressDel($id, $user_id);
            if ($res) {
                return Response::wrapData();
            } else {
                return Response::wrapError("删除操作失败!");
            }
        }
    }
}